package com.mak001.lite.multiSkill.subs.Miners;

import org.rsbuddy.tabs.Inventory;
import org.rsbuddy.widgets.Bank;

import extras.mak001.AnimWatcher;
import com.mak001.lite.multiSkill.Methods;
import extras.mak001.Time;
import com.mak001.lite.multiSkill.subScript;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.wrappers.Area;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Tile;

public class StairMiner extends subScript {

	Area bankArea;
	Area stairArea;
	Area stairArea2;
	Tile mineTile;
	int[] rockID;
	int pick = 1275;
	int upStair;
	int downStair;

	boolean switchB;

	public void setSwitch(boolean b) {
		if (switchB != b)
			switchB = b;
	}

	public enum state {
		BANK, WALKTOBANK, MINE, WALKTOMINE, DOWNSTAIRS, UPSTAIRS, WALKTOSTAIRS
	}

	public state getState() {
		if (Inventory.isFull()) {
			if (bankArea.contains(Players.getLocal().getLocation())) {
				return state.BANK;
			} else {
				if (stairArea2.contains(Players.getLocal().getLocation())) {
					return state.UPSTAIRS;
				} else if (Calculations.distanceTo(stairArea2.getCentralTile()) < 30) {
					return state.WALKTOSTAIRS;
				} else {
					return state.WALKTOBANK;
				}
			}
		} else {
			if (Methods.andOr(Objects.getNearest(rockID) != null, Calculations
					.distanceTo(mineTile) < 20)) {
				return state.MINE;
			} else {
				if (stairArea.contains(Players.getLocal().getLocation())) {
					return state.DOWNSTAIRS;
				} else {
					return state.WALKTOMINE;
				}
			}
		}
	}

	@Override
	public boolean canSwitch() {
		return switchB;
	}

	@Override
	public void execute() {
		if (bankArea == null) {
			bankArea = Methods.getNearestBank(stairArea.getCentralTile());
		} else {
			switch (getState()) {
			case WALKTOBANK:
				Walking.findPath(bankArea.getCentralTile()).traverse();
				break;
			case BANK:
				setSwitch(true);
				if (Bank.isOpen()) {
					Bank.depositAllExcept(pick);
				} else {
					Bank.open();
				}
				break;
			case DOWNSTAIRS:
				GameObject DS = Objects.getNearest(downStair);
				if (DS != null) {
					if (DS.isOnScreen()) {
						DS.interact("");
					} else {
						Walking.findPath(DS).traverse();
					}
				} else if (Objects.getNearest(26933) != null) {
					Objects.getNearest(26933).interact("");
				}
				break;
			case UPSTAIRS:
				GameObject US = Objects.getNearest(upStair);
				if (US != null) {
					if (US.isOnScreen()) {
						US.interact("");
					} else {
						Walking.findPath(US).traverse();
					}
				}
				break;
			case MINE:
				if (rock() != null && AnimWatcher.isIdle()
						&& !Players.getLocal().isMoving()) {
					if (rock().isOnScreen()) {
						if (rock().interact("")) {
							sleep(1000);
						}
					} else {
						Walking.findPath(rock().getLocation()).traverse();
					}
				}
				break;
			case WALKTOMINE:
				setSwitch(false);
				if (Objects.getNearest(11714) != null
						&& Objects.getNearest(11714).getLocation().equals(
								new Tile(3061, 3374))) {
					Objects.getNearest(11714).interact("");
				} else {
					Walking.findPath(stairArea.getCentralTile()).traverse();
				}
				break;
			case WALKTOSTAIRS:
				Walking.findPath(stairArea2.getCentralTile()).traverse();
				break;

			}
		}
	}

	public GameObject rock() {
		return Objects.getNearest(rockID);
	}
	
	@Override
	public int[] required() {
		int[] req = { pick };
		// TODO Auto-generated method stub
		return req;
	}

	public void setArea(String s) {
		if (s.contains("edgville")) {
			this.stairArea = new Area(new Tile(3094, 3466),
					new Tile(3099, 3471));
			this.stairArea2 = new Area(new Tile(3095, 6865), new Tile(3100,
					9870));
			this.downStair = 29634;
			this.upStair = 29355;
			this.mineTile = new Tile(3139, 9873);
		} else if (s.contains("dwarven mine")) {
			this.stairArea = new Area(new Tile(3057, 3374),
					new Tile(3062, 3379));
			this.stairArea2 = new Area(new Tile(3056, 9773), new Tile(3062,
					9780));
			this.downStair = 30944;
			this.upStair = 30943;
			this.mineTile = new Tile(3046, 9772);
		} else if (s.contains("mining guild")) {
			this.stairArea = new Area(new Tile(3016, 3336),
					new Tile(3022, 3342));
			this.stairArea2 = new Area(new Tile(3017, 9737), new Tile(3024,
					9742));
			this.downStair = 2113;
			this.upStair = 6226;
			this.mineTile = new Tile(3043, 9737);
		}
	}

	public void setRock(int[] rocks) {
		this.rockID = rocks;
	}

	@Override
	public String getStatus() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int skill() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void messageRecieved(MessageEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String[] paintString() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Time getTime() {
		// TODO Auto-generated method stub
		return null;
	}

}
